package com.xbongbong.saas.model.impl;

import com.xbongbong.paas.constant.PaasConstant;
import com.xbongbong.paas.enums.DelEnum;
import com.xbongbong.paas.toolbox.exception.XbbException;
import com.xbongbong.paas.toolbox.util.DateUtil;
import com.xbongbong.pro.domain.dao.IndexSetDao;
import com.xbongbong.pro.domain.entity.IndexSetEntity;
import com.xbongbong.saas.model.IndexSetModel;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description: 首页看板
 * @Author: hongxiao
 * @date: 2019-02-27 16:18
 * @since v1.0
 */
@Service("indexSetModel")
public class IndexSetModelImpl implements IndexSetModel {

    @Resource
    private IndexSetDao indexSetDao;

    @Override
    public Integer insert(IndexSetEntity entity) throws XbbException {
        long now = DateUtil.getInt();
        entity.setAddTime(now);
        entity.setUpdateTime(now);

        return indexSetDao.insert(entity);
    }

    @Override
    public Integer update(IndexSetEntity entity) throws XbbException {
        entity.setUpdateTime((long)DateUtil.getInt());
        return indexSetDao.update(entity);
    }

    @Override
    public void insertBatch(List<IndexSetEntity> entityList) {
        indexSetDao.insertBatch(entityList);
    }

    @Override
    public Integer save(IndexSetEntity entity) throws XbbException {
        if(entity.getId() == null || entity.getId().equals(0L)) {
            return insert(entity);
        }
        return update(entity);
    }

    @Override
    public List<IndexSetEntity> findEntitys(Map<String, Object> param) {
        return indexSetDao.findEntitys(param);
    }

    @Override
    public void updateBatch(List<IndexSetEntity> list, String corpid) throws XbbException {
        indexSetDao.updateBatch(list, corpid);
    }

    @Override
    public void updateBatchNoCorpId(List<IndexSetEntity> list) {
        indexSetDao.updateBatchNoCorpId(list);
    }

    @Override
    public Integer getEntitysCount(Map<String, Object> param) {
        return indexSetDao.getEntitysCount(param);
    }

    @Override
    public List findEntitysWithoutSub(Map<String, Object> param) {
        return null;
    }

    @Override
    public IndexSetEntity getByBoardType(String corpid, String userId, Integer boardType) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("userId", userId);
        param.put("boardType", boardType);
        param.put("del", DelEnum.NORMAL.getDel());
        param.put("notEqualCustom",1);

        List<IndexSetEntity> list = findEntitys(param);
        if(list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    public List<IndexSetEntity> getCompanyConfig(String corpid) {
        Map<String, Object> param = new HashMap<>(PaasConstant.DEFAULT_INITIAL_CAPACITY);
        param.put("corpid", corpid);
        param.put("userId", "0");
        param.put("del", DelEnum.NORMAL.getDel());
        return findEntitys(param);
    }

    @Override
    public List<IndexSetEntity> findByBiScript(String corpid) {
        return indexSetDao.findByBiScript(corpid);
    }

    @Override
    public IndexSetEntity getByKey(Long key, String corpid) {
        return indexSetDao.getByKey(key, corpid);
    }

    @Override
    public Integer deleteFocusBatchByCorpids4Script(List<String> list, List<Integer> boardTypeList) {
        return indexSetDao.deleteFocusBatchByCorpids4Script(list,boardTypeList);
    }
}
